﻿package ReadWriteXML;

import java.io.File;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import com.app.DTO.RoleDTO;
import com.app.PhanQuyen.Quyen;

import android.os.Environment;
import android.util.Log;

public class ReadXML {

	public static Document BuildXML(String path) {
		Document doc = null;
		DocumentBuilder builder = null;
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		try {
			builder = factory.newDocumentBuilder();
			doc = builder.newDocument();

		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		}

		File f = new File(path);
		if (f.exists() == true) {
			doc = DOMUltil.parse(path);

		}

		return doc;
	}

	public static String getTextValue(Element e, String tagName) {
		String textVal = null;
		Node parent = e.getParentNode();
		NodeList nl = e.getElementsByTagName(tagName);
		if (nl != null && nl.getLength() > 0) {
			Element el = (Element) nl.item(0);
			textVal = el.getFirstChild().getNodeValue();
		}
		return textVal;
	}

	/**
	 * Calls getTextValue and returns a int value
	 */
	public static int getIntValue(Element e, String tagName) {
		// in production application you would catch the exception
		return Integer.parseInt(getTextValue(e, tagName));
	}

	public static String getTextValue(String path, String tagName) {
		Document doc = null;
		doc = BuildXML(path);
		String text = null;
		if (doc != null) {
			Node n = doc.getElementsByTagName(tagName).item(0);
			text = n.getTextContent();

		}
		return text;
	}

	public static int getIntValue(String path, String tagName) {
		Log.d("PEERDROID", "tagName: " + tagName);
		Document doc = null;
		doc = BuildXML(path);
		String text = null;
		if (doc != null) {
			Node n = doc.getElementsByTagName(tagName).item(0);
			text = n.getTextContent();
			Log.d("PEERDROID", "text: " + text);
		}
		return Integer.valueOf(text);
	}

	public static String getTextValue(String path, String node, String attribute) {
		Document doc = null;
		doc = BuildXML(path);
		String text = null;
		if (doc != null) {
			Element e = (Element) doc.getElementsByTagName(node).item(0);
			text = e.getAttribute(attribute);
		}
		return text;
	}

	public static ArrayList<String> getTextValueElement(String path,
			String element, String attribute) {
		Document doc = null;
		doc = BuildXML(path);
		ArrayList<String> strList = new ArrayList<String>();

		if (doc != null) {
			NodeList nlist = doc.getElementsByTagName(element);
			int n = nlist.getLength();
			for (int i = 0; i < n; i++) {
				Element e = (Element) nlist.item(i);
				String text = e.getAttribute(attribute);
				strList.add(text);
			}
		}
		return strList;
	}

	
}